package com.view;

/**************************************************** IMPORT ****************************************************/
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

import com.diana.MainActivity;
import com.diana.R;


public class ArchiveScreen extends Screen implements OnTouchListener{

	/************ DECLARATIONS ***************/
	private Button archive1, archive2;
	float initX, currentX, translation, valueAlpha;
	int actionTouch, screenWidth, translationToDelete;
	private Display screenSize;
	String textX; 
	
	
	/************ CONSTRUCTOR ****************/
	public ArchiveScreen(MainActivity activity) {
		super(activity,R.layout.archive);
	}
	
	
	/************** METHODES *****************/
	/**
	 * @brief Instantiation of widgets and listeners.
	 */
	public void recuperationWidgetInterface(){
		setArchive1((Button)getActivity().findViewById(R.id.noteArchive));
		archive1.setOnTouchListener(this);
		
		setArchive2((Button)getActivity().findViewById(R.id.noteArchive2));
	}
	
	/**
	 * @brief Find the size of the screen.
	 */
	@SuppressWarnings("deprecation")
	public void sizeScreen(){
		screenSize = getActivity().getWindowManager().getDefaultDisplay();
		screenWidth = screenSize.getWidth();
	}

	/**
	 * @brief Show the view.
	 */
	@Override
	public void display() {
		recuperationWidgetInterface();
	}

	/**
	 * @brief Close the view.
	 */
	@Override
	public void close() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @brief Touch on button of the archive view.
	 * @param v : current view
	 * @param event : event detected (up, down or move).
	 */
	@SuppressWarnings("deprecation")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		sizeScreen();
		translationToDelete = screenWidth/3;
		valueAlpha = 1f;
		
		actionTouch = event.getAction();
		if(actionTouch==MotionEvent.ACTION_DOWN)
		{
			initX = event.getX();
			archive1.setSelected(true);
		}
		if(actionTouch==MotionEvent.ACTION_MOVE)
		{
			archive1.setSelected(true);
			currentX = event.getX();
			translation = currentX - initX;
			textX = String.valueOf(translation);
			archive2.setText(textX);
			opacityNote(translation);
		}
		if(actionTouch==MotionEvent.ACTION_UP)
		{
			archive1.setSelected(false);
			translation = currentX - initX;
			deleteNote(translation);
		}
		return true;
	}

	/**
	 * @brief Change the opacity of the note.
	 * @param translation : translation of the note
	 */
	private void opacityNote(float translation) {
		if(translation < translationToDelete)
		{
			valueAlpha = 1-(translation/translationToDelete);
		}
		else
		{
			valueAlpha = 0f;
		}
    	//0f transparent / 1f opaque
    	archive1.setAlpha(valueAlpha);
	}

	/**
	 * @brief Delete the note.
	 * @param translation : translation of the note
	 */
	private void deleteNote(float translation) {
	    if(translation >= translationToDelete)
	    {
	    	archive1.setVisibility(View.GONE);
	    }
	}

	
	/************* ACCESSORS *************/
	public Button getArchive1() {
		return archive1;
	}

	public void setArchive1(Button archive) {
		this.archive1 = archive;
	}
	
	public Button getArchive2() {
		return archive2;
	}

	public void setArchive2(Button archive) {
		this.archive2 = archive;
	}
}
